package model.dao;

import java.util.List;

import model.entity.Questionario;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 *
 * Questionario DAO
 *
 * @author Alex
 *
 */

@Repository(value="questionarioDAO")
public class QuestionarioDAO implements IQuestionarioDAO {

	@Autowired
	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 *
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 *
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * Add Questionario
	 *
	 * @param Questionario
	 *            questionario
	 */
	@Override
	public void addQuestionario(Questionario questionario) {
		getSessionFactory().getCurrentSession().save(questionario);
	}

	/**
	 * Delete Questionario
	 *
	 * @param Questionario
	 *            questionario
	 */
	@Override
	public void deleteQuestionario(Questionario questionario) {
		getSessionFactory().getCurrentSession().delete(questionario);
	}

	/**
	 * Update Questionario
	 *
	 * @param Questionario
	 *            questionario
	 */
	@Override
	public void updateQuestionario(Questionario questionario) {
		getSessionFactory().getCurrentSession().update(questionario);
	}

	/**
	 * Get Questionario
	 *
	 * @param int Questionario Id
	 * @return Questionario
	 */
	@Override
	public Questionario getQuestionarioById(int id) {
		@SuppressWarnings("unchecked")
		List<Questionario> list = getSessionFactory().getCurrentSession()
				.createQuery("from Questionario where id=?")
				.setParameter(0, id).list();
		return (list.isEmpty()) ? null : list.get(0);
	}

	/**
	 * Get Questionario List
	 *
	 * @return List - Questionario list
	 */
	@Override
	public List<Questionario> getQuestionarios() {
		@SuppressWarnings("unchecked")
		List<Questionario> list = getSessionFactory().getCurrentSession()
				.createQuery("from Questionario").list();
		return list;
	}

}
